package com.lie.flat.service.connect;

import com.lie.flat.entity.CardRequest;
import com.lie.flat.entity.CardResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class ConnectService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 向第三方接口发送卡列表信息请求
     *
     * @param cardRequest
     * @return
     */

    public CardResponse jsonConCard(CardRequest cardRequest) {

        final String url = "http://139.196.200.240:8070/creditCardTest/getCard";
        CardResponse cardResponse = null;

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        HttpEntity<CardRequest> entity = new HttpEntity<>(cardRequest, httpHeaders);
        try {
            ResponseEntity<CardResponse> resp = restTemplate.postForEntity(url, entity, CardResponse.class);
            cardResponse = resp.getBody();
        } catch (Exception e) {
            log.error("请求失败", e);
        }

        return cardResponse;

    }
}
